import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useCounterStore = defineStore('counter', () => {
  let carts=ref([])
  let id=ref(0)
  function addcart(detaildata,detailgoodsid){
    let username=localStorage.getItem('username')
    let userCarts=carts.value.filter(item=>item.username==username)
    let findcart=userCarts.find(item=>item.goods.goods_id==detailgoodsid)
    if(findcart){
      findcart.number++
    }else{
      carts.value.push({
        username:username,
        id:id.value++,
        number:1,
        checked:false,
        goods:detaildata
      })
    }
  }
  function delcart(id){
    carts.value=carts.value.filter(item=>item.id!=id)
  }
  return { carts, addcart, delcart }
})